<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>{$appConfigs.title}</title>
    <link rel="stylesheet" type="text/css" href="{$contextPath}/pas/easyui/themes/{$page_theme | default: 'default'}/easyui.css">
    <link rel="stylesheet" type="text/css" href="{$contextPath}/pas/easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="{$contextPath}/pas/easyui/themes/color.css">
    <script type="text/javascript" src="{$contextPath}/pas/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="{$contextPath}/pas/easyui/jquery.easyui.min.js"></script>
</head>
<body onpageshow="resetPagination()">
    <table id="dg" title="所有附件" class="easyui-datagrid" style="width:99%;height:95%"
            url="{$contextPath}/cms/attachment/findAll.xdo"
            toolbar="#toolbar" pagination="true"
            rownumbers="true" fitColumns="true" singleSelect="false">
        <thead>
            <tr>
                <th field="name" width="15" formatter="nameFormatter">文件名</th>
                <th field="size" width="8">大小</th>
                <th field="content_type" width="10">Content Type</th>
                <th field="tag" width="10">标签</th>
                <th field="create_date" width="15">创建时间</th>
                <th field="path" width="30">路径</th>
                <th field="desc" width="40">说明</th>
                <th field="url" width="11" formatter="urlFormatter">操作</th>
            </tr>
        </thead>
    </table>
    <div id="toolbar">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="createOne()">新建</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="updateOne()">修改</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removeOne()">删除</a>
    </div>
    
    <div id="dlg" class="easyui-dialog" style="width:600px;height:450px;padding:10px 20px"
            closed="true" buttons="#dlg-buttons">
        <div class="ftitle">附件信息</div>
        <form id="fm" method="post" enctype='multipart/form-data' novalidate>
            <div class="fitem" id="fileitem">
                <label>文件:</label>
                <input name="file" type="file" style="width:300px" multiple="multiple">
            </div>
            <div class="fitem" id="filename">
                <label>文件名:</label>
                <input name="name" class="easyui-textbox" style="width:300px">
            </div>
            <div class="fitem" id="tagname">
                <label>标签:</label>
                <input name="tag" class="easyui-textbox" style="width:300px">
            </div>
            <div class="fitem" id="filedesc">
                <label>说明:</label>
                <input name="desc" class="easyui-textbox" multiline="true" style="width:300px;height:180px">
            </div>
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveOne()" style="width:90px">保存</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">取消</a>
    </div>
    <script type="text/javascript">
		var uploadURL = '{$uploadURL}';
    {literal}
        var url;
        function createOne(){
            $('#dlg').dialog('open').dialog('setTitle','新增');
            $('#fm').form('clear');
            $('#fileitem').show();
            $('#filename').hide();
            $('#filedesc').hide();
            url = '/cms/attachment/uploads.xdo';
        }
        function updateOne(){
            var row = $('#dg').datagrid('getSelected');
            if (row){
                $('#dlg').dialog('open').dialog('setTitle','修改');
                $('#fm').form('load',row);
            	$('#fileitem').hide();
	            $('#filename').show();
	            $('#filedesc').show();
                url = '/cms/attachment/update.xdo?attachment_id='+row.attachment_id;
            }
        }
        function saveOne(){
            $('#fm').form('submit',{
                url: url,
                onSubmit: function(){
                    return $(this).form('validate');
                },
                success: function(result){
                    var result = eval('('+result+')');
                    if (result.errorMsg){
                        $.messager.show({
                            title: 'Error',
                            msg: result.errorMsg
                        });
                    } else {
                        $('#dlg').dialog('close');        // close the dialog
                        $('#dg').datagrid('reload');    // reload the attachment data
						$('#parent_id').combotree('reload');
                    }
                }
            });
        }
        function removeOne(){
            var selections = $('#dg').datagrid('getSelections');
            if (selections.length > 0 ){
            	var rows = new Array();
            	for(var i=0; i< selections.length; i++){
            		rows.push(selections[i].attachment_id);
            	}
                $.messager.confirm('Confirm','删除后数据不能恢复，确认删除么?',function(r){
                    if (r){
                        $.post('/cms/attachment/remove.xdo',{'attachment_id':rows},function(result){
                            if (result.success){
                                $('#dg').datagrid('reload');    // reload the attachment data
								$('#parent_id').combotree('reload');
                            } else {
                                $.messager.show({    // show error message
                                    title: 'Error',
                                    msg: result.errorMsg
                                });
                            }
                        },'json');
                    }
                });
            }
        }
        function copy_code(copyText) { 
			if (window.all) { 
				window.clipboardData.setData("Text", copyText);
				alert('copy成功！'); 
			} else { 
				alert('您的浏览器不支持剪贴板操作，请自行复制。'); 
			} 
		}
		function nameFormatter(value, row, index){
			if(!value) value ="";
			return '<a href=\"' + uploadURL + "/" + row.path+'\" target="blank"><img height=40 width=40 src=\"' + uploadURL + "/" + row.path+'\">'+value+'</a>';
		}
		function urlFormatter(value, row, index){
			if(!value) value ="";
			return '<a type=\"button\" href=\'javascript:copy_code(\"' + uploadURL + "/" + row.path+'\")\' >复制</a>';
		}
		function resetPagination(){
			var p = $('#dg').datagrid('getPager');
			$(p).pagination({             
				pageSize: 15,           
		 		pageList: [15,30],
				beforePageText: '第',      
				afterPageText: '页    共 {pages} 页',             
				displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'                  
			}); 
		}
    </script>
    <style type="text/css">
        #fm{
            margin:0;
            padding:10px 30px;
        }
        .ftitle{
            font-size:14px;
            font-weight:bold;
            padding:5px 0;
            margin-bottom:10px;
            border-bottom:1px solid #ccc;
        }
        .fitem{
            margin-bottom:5px;
        }
        .fitem label{
            display:inline-block;
            width:80px;
        }
        .fitem input{
            width:160px;
        }
    </style>
    {/literal}
</body>
</html>